package com.fanjia.crm.web.controller;

import com.fanjia.crm.model.CURDResult;
import com.fanjia.crm.model.PageResult;
import com.fanjia.crm.model.User;
import com.fanjia.crm.service.IRoleService;
import com.fanjia.crm.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by FanJia on 2020/9/22 20:14
 */
@Controller
@RequestMapping(value = "/user")
public class UserController {

    @Autowired
    IUserService userService;
    @Autowired
    IRoleService roleService;

    @RequestMapping("/list")
    public String list(){
        return "/user/list";
    }

    @RequestMapping("/add")
    public String add(Model model){
        model.addAttribute("roles",roleService.findAllRoles());
        return "/user/add";
    }

    @RequestMapping("/edit")
    public String edit(Model model,Integer userId){
        //获取角色数据
        model.addAttribute("roles",roleService.findAllRoles());
        User user = userService.findUserAndRoleByUserId(userId);
        System.out.println("userID" + userId);
        model.addAttribute("user",user);
        return "/user/edit";
    }

    @RequestMapping("/save")
    @ResponseBody
    public CURDResult save(User user,Integer[] roleIds){
        CURDResult result = new CURDResult();
        userService.save(user,roleIds);

        return result;
    }

    @RequestMapping("/listjson")
    @ResponseBody
    public PageResult<User> listJson(User user,int page,int limit){

        return userService.findPageResult(user,page,limit);

    }

}
